package com.netease.file;

import net.sf.json.JSONArray;

import java.io.*;

/**
 * @desctiption
 * @Author bjweitingting
 * @date 2018/5/10
 **/
public class Main {
    public static void main(String[] args) {
        String path = "C:/Users/bjweitingting/Desktop//20180509/";
        //读取文件
        String messageContent = readFile(path + "/message.txt");
        String sessionContent = readFile(path + "/session.txt");
        //处理聊天内容
        int start = messageContent.indexOf("startTime=");
        if (start > 0) {
            messageContent = messageContent.substring(0, start - 1);
        }
         start = sessionContent.indexOf("startTime=");
        if (start > 0) {
            sessionContent = sessionContent.substring(0, start - 1);
        }
//        messageContent=messageContent.substring(1,messageContent.length());
        messageContent = "[" + messageContent + "]";
        //
        sessionContent = "[" + sessionContent + "]";
        messageContent.replace("\n", ",");

        JSONArray messageArray = JSONArray.fromObject(messageContent);
        JSONArray sessionArray = JSONArray.fromObject(sessionContent);
        System.out.println("test");
    }

    /**
     * read file。保留换行符
     *
     * @param path
     * @return
     */
    public static String readFile(String path) {
        File file = new File(path);
        StringBuffer buf = new StringBuffer();
        BufferedReader reader = null;
        String line = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            while ((line = reader.readLine()) != null) {
//                JSONObject jsonObject = JSONObject.fromObject(line);
                line += "\n";
                buf.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
        return buf.toString();
    }
}
